<template>
  <div>
    <div>
      <BaseDialog width="60%" :visible.sync="visibleEmploy" title="工时选择" destroyOnClose :containerStyle="{ height: 'calc(100% - 70px)', overflow: 'auto', paddingBottom: '70px' }">
        <workhoursFrom @selectedRow="selectWorKhours" @close="closeWorkhours" />
      </BaseDialog>
      <FormPanel ref="formPanel" :list="fromList" :cols="1" labelWidth="130" :isSubmitBtn="false"></FormPanel>
    </div>
    <div :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }">
      <AjaxButton type="primary" :disabled="newStatus" :click="saveDialog">保 存</AjaxButton>
      <el-button @click="closeDialog">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import { queryModel, addModel } from '@/api/repair/repairBusiness/model/index.js';
import { getBrand, getSeries, getModel } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';
import { getConfigHeaders } from '@/api/fetch';
import { jsonToHump } from '@/utils/index';
import workhoursFrom from './workhoursForm'
import { dictionary } from '@/utils/dictMixin';
import { addLabour, editLabour, getListByPage } from '@/api/claim/dealer/AuthMaintenance';
export default {
  props: ['row', 'type','currentUserInfo'],
  mixins: [dictionary],
  components: {
    workhoursFrom
  },
  data() {
    return {
      fromList: this.createFromList(),
      fileList: [],
      getConfigHeaders: getConfigHeaders,
      newStatus: false,
      formLabelPro: {},
      visibleEmploy: false,//员工选择页面
      selectWorKhoursList: [],
      levelData: []
    };
  },

  computed: {
  },
  mounted() {
    if (this.type == 'edit') {
      this.fromList.forEach(x => {
        x.initialValue = this.row[x.fieldName];
      });;
    }
  },
  created() {
    // this.fromList.find((x) => x.fieldName == 'powerType').itemList = this.createDictList('6008');

  },
  methods: {
    //获取预授权级别
    getLevelData() {
      let arr = []
      getListByPage().then(res => {
        if (res.data) {
          res.data.forEach((item, i) => {
            arr.push({ text: `${item.name}-${this.createDictText(item.authType,'5508')}`, value: item.id })
          })
          this.levelData = arr;
        }
      })
      return arr
    },
    createFromList() {
      return [
        {
          type: 'INPUT',
          label: '工时代码',
          fieldName: 'lbCode',
          readonly: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.addWorkhours()}></el-button>
              </div>
            );
          },
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
        {
          type: 'INPUT',
          label: '工时名称',
          fieldName: 'lbName',
          readonly: true,
          disabled: true,
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
        {
          type: 'SELECT',
          label: '预授权级别',
          fieldName: 'name',
          itemList: this.getLevelData(),
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        }
      ];
    },
    //选择工时
    selectWorKhours(val) {
      this.selectWorKhoursList = val
      let code = []
      let name = []
      val.forEach(function (item, index) {
        code.push(item.labourCode)
        name.push(item.labourName)
      })
      this.visibleEmploy = false
      this.fromList.find(x => x.fieldName === 'lbCode').initialValue = code.join(',');
      this.fromList.find(x => x.fieldName === 'lbName').initialValue = name.join(',');
    },
    //选择工时页面
    addWorkhours() {
      this.visibleEmploy = true
    },
    async saveDialog() {
      const [err, data] = await this.$refs.formPanel.GET_FORM_DATA();// 获取from改变时整个表单的值
      if (err) return;
      let params = JSON.parse(JSON.stringify(data))
      // jsonToHump(json);
      console.log(this.levelData,params)
      params = Object.assign({},this.row,params);
      params.createdBy = this.currentUserInfo.createdById;
      this.levelData.forEach((item) => {
        if (item.text === params.name || item.value === params.name) {
          params.lId = item.value;
          params.name = item.text;
        }
      });
      if (this.type === 'add') {
        //新增
        let res = await addLabour(params);
        if (res.resultCode === 200) {
          this.$notify.success({ type: '提示信息', message: '保存成功!!' });
          this.$emit('close');
        }
      } else if (this.type === 'edit') {
        //编辑
        params.id = this.row.id;
        params.updatedBy = this.currentUserInfo.updatedById;
        let res = await editLabour(params);
        if (res.resultCode === 200) {
          this.$notify.success({ type: '提示信息', message: '修改成功!!' });
          this.$emit('close');
        }
      }

    },
    closeDialog() {
      this.$emit('close');
    },
    closeWorkhours() {
      this.visibleEmploy = false
    }
  }
};
</script>
